<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * SPARQL result set
 * 
 * Modelled around: http://www.w3.org/TR/rdf-sparql-json-res/
 */
class triplestore_SparqlResultSet
{
	protected $variables = null;
	protected $results = null;
	protected $boolean = null;
	
	/**
	 * Construct a SPARQL result set object.
	 * @param string $data Raw result set
	 * @param string $format Raw result set format (currently only 'application/sparql-results+json' is fully implemented, 'application/sparql-results+xml' is only able to parse boolean result sets)
	 * @throws triplestore_exception_QueryFailed
	 */
	public function __construct($data, $format)
	{
		if($format === 'application/sparql-results+json')
		{
			$data = json_decode($data, true);
			if($data === null)
			{
				throw new triplestore_exception_QueryFailed('Unable to parse results from server', null);
			}

			$this->variables = @$data['head']['vars'];
			$this->results = @$data['results']['bindings'];
			$this->boolean = @$data['boolean'];
		}
		else // assume 'application/sparql-results+xml'
		{
			// sparql xml result set
			//TODO: currently only supports boolean results
			//TODO: handle parser errors?
			$doc = new DOMDocument();
			$doc->loadXML($data);
			$this->boolean = $doc->getElementsByTagName('boolean')->item(0)->textContent == 'true';
		}
	}
	
	/**
	 * Gets the set of Query Variable names in the Solution Sequence.
	 * @return array Array of Query Variable names
	 */
	public function getVariables()
	{
		return $this->variables;
	}
	
	/**
	 * Get the query results for a SPARQL select query.
	 * 
	 * The result set is a nested array structure:
	 * - For each Query Solution in the query result an array is added
	 * - The keys of these arrays are the names of the Query Variables, the values are arrays
	 * - These arrays contain two keys: "type" and "value":
	 *   - "type" contains either "uri" or "literal"
	 *   - "value" contains the value of the variable
	 * @return array Result array
	 */
	public function getResults()
	{
		return $this->results;
	}

	/**
	 * Get the query results for a SPARQL ask query.
	 * @return boolean Result boolean
	 */
	public function getBoolean()
	{
		return $this->boolean;
	}
}